package com.getpebble.android.jskit;

import android.text.TextUtils;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.jskit.bridge.JsAppContextBridge;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.json.JSONObject;

public final class JsAppPersistentSettings {
    public static final UUID NullUuid = new UUID(0, 0);
    private Lock mAccessLock;
    private JSONObject mCurrentAppLocalStorage;
    private JSONObject mCurrentAppUserSpaceSettingsAnyVersion;
    private Map<String, JSONObject> mCurrentAppUserSpaceSettingsPerVersion;
    private JSONObject mCurrentSystemSettings;
    private final UUID mJsAppUuid;

    private static class AllJsAppPersistentSettingsRef {
        public static Map<UUID, JsAppPersistentSettings> INST = new HashMap();
    }

    public enum LaunchOnMessageSettingE {
        _INVALID_,
        NotDeclared,
        Yes,
        No
    }

    public JsAppPersistentSettings() {
        this.mAccessLock = new ReentrantLock();
        this.mCurrentSystemSettings = null;
        this.mJsAppUuid = null;
        this.mCurrentAppUserSpaceSettingsAnyVersion = null;
        this.mCurrentAppUserSpaceSettingsPerVersion = null;
    }

    private JsAppPersistentSettings(UUID jsAppUuid, JSONObject fromSystemPrefs, JSONObject appAnyVersionObject, JSONObject appLocalStorageObject) {
        this.mAccessLock = new ReentrantLock();
        this.mCurrentSystemSettings = fromSystemPrefs;
        this.mJsAppUuid = jsAppUuid;
        if (appAnyVersionObject == null) {
            appAnyVersionObject = new JSONObject();
        }
        this.mCurrentAppUserSpaceSettingsAnyVersion = appAnyVersionObject;
        if (appLocalStorageObject == null) {
            appLocalStorageObject = new JSONObject();
        }
        this.mCurrentAppLocalStorage = appLocalStorageObject;
        if (this.mCurrentAppUserSpaceSettingsPerVersion == null) {
            this.mCurrentAppUserSpaceSettingsPerVersion = new HashMap();
        }
    }

    public static Map<UUID, JsAppPersistentSettings> allJsAppPersistentSettingsRef() {
        return AllJsAppPersistentSettingsRef.INST;
    }

    public static JsAppPersistentSettings tryLoad(UUID jsAppUuid) {
        return tryLoad(jsAppUuid, "*", false);
    }

    public static JsAppPersistentSettings tryLoad(UUID jsAppUuid, int specificVersion) {
        return tryLoad(jsAppUuid, "" + specificVersion, false);
    }

    public static JsAppPersistentSettings tryLoad(UUID jsAppUuid, String specificVersion, boolean reload) {
        Throwable e;
        if (jsAppUuid == null) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = (JsAppPersistentSettings) allJsAppPersistentSettingsRef().get(jsAppUuid);
        if (reload || jsAppPersistentSettings == null) {
            jsAppPersistentSettings = null;
            try {
                JSONObject appUserSpaceAnyVersionJsonObject = getStoredJsonizedData(formAppUserSpaceKeystring(jsAppUuid, "*"));
                JSONObject systemPrefsJsonObject = getStoredJsonizedData(formKeystring(jsAppUuid));
                JSONObject localStorageJsonObject = getStoredJsonizedData(formAppLocalStorageKeystring(jsAppUuid));
                if (systemPrefsJsonObject != null && systemPrefsJsonObject.has("v") && systemPrefsJsonObject.has("u") && UUID.fromString(systemPrefsJsonObject.optString("u", "")).equals(jsAppUuid)) {
                    JsAppPersistentSettings jsAppPersistentSettings2 = new JsAppPersistentSettings(jsAppUuid, systemPrefsJsonObject, appUserSpaceAnyVersionJsonObject, localStorageJsonObject);
                    try {
                        synchronized (allJsAppPersistentSettingsRef()) {
                            allJsAppPersistentSettingsRef().put(jsAppUuid, jsAppPersistentSettings2);
                        }
                        jsAppPersistentSettings = jsAppPersistentSettings2;
                    } catch (Exception e2) {
                        e = e2;
                        jsAppPersistentSettings = jsAppPersistentSettings2;
                    }
                }
            } catch (Exception e3) {
                e = e3;
                Trace.error("JsAppPersistentSettings", e);
                if (jsAppPersistentSettings == null) {
                    jsAppPersistentSettings = new JsAppPersistentSettings(jsAppUuid, defaultSettings(jsAppUuid), null, null);
                    synchronized (allJsAppPersistentSettingsRef()) {
                        allJsAppPersistentSettingsRef().put(jsAppUuid, jsAppPersistentSettings);
                        jsAppPersistentSettings.modifyStoredSystemPreference("tryLoad-corrupt-recreate");
                        jsAppPersistentSettings.modifyStoredAppUserSpaceAnyVersionPreference("tryLoad-corrupt-recreate");
                        jsAppPersistentSettings.modifyStoredAppLocalStoragePreference("tryLoad-corrupt-recreate");
                    }
                }
                return specificVersion.equalsIgnoreCase("*") ? jsAppPersistentSettings : jsAppPersistentSettings;
            }
            if (jsAppPersistentSettings == null) {
                jsAppPersistentSettings = new JsAppPersistentSettings(jsAppUuid, defaultSettings(jsAppUuid), null, null);
                synchronized (allJsAppPersistentSettingsRef()) {
                    allJsAppPersistentSettingsRef().put(jsAppUuid, jsAppPersistentSettings);
                    jsAppPersistentSettings.modifyStoredSystemPreference("tryLoad-corrupt-recreate");
                    jsAppPersistentSettings.modifyStoredAppUserSpaceAnyVersionPreference("tryLoad-corrupt-recreate");
                    jsAppPersistentSettings.modifyStoredAppLocalStoragePreference("tryLoad-corrupt-recreate");
                }
            }
        }
        if (specificVersion.equalsIgnoreCase("*") && !jsAppPersistentSettings.loadAppUserSpaceSpecificVersionSettings(specificVersion)) {
            return null;
        }
    }

    private boolean load() {
        boolean appUserSpaceSettingsAnyVersionLoaded = loadAppUserSpaceAnyVersionSettings();
        if (this.mJsAppUuid != null) {
            try {
                this.mCurrentSystemSettings = getStoredJsonizedData(myKeyString());
                if (this.mCurrentSystemSettings == null) {
                    this.mCurrentSystemSettings = defaultSettings(this.mJsAppUuid);
                }
            } catch (Throwable e) {
                Trace.error("JsAppPersistentSettings", e);
            }
            try {
                this.mCurrentAppUserSpaceSettingsAnyVersion = getStoredJsonizedData(myAppUserSpaceKeyString());
                if (this.mCurrentAppUserSpaceSettingsAnyVersion == null) {
                    this.mCurrentAppUserSpaceSettingsAnyVersion = new JSONObject();
                }
            } catch (Throwable e2) {
                Trace.error("JsAppPersistentSettings", e2);
            }
            try {
                this.mCurrentAppLocalStorage = getStoredJsonizedData(myAppLocalStorageKeyString());
                if (this.mCurrentAppLocalStorage == null) {
                    this.mCurrentAppLocalStorage = new JSONObject();
                }
            } catch (Throwable e22) {
                Trace.error("JsAppPersistentSettings", e22);
            }
            if (this.mCurrentSystemSettings != null) {
                modifyStoredSystemPreference("load-default-create;system");
            }
            if (this.mCurrentAppUserSpaceSettingsAnyVersion != null) {
                modifyStoredAppUserSpaceAnyVersionPreference("load-default-create;anyver");
            }
            if (this.mCurrentAppLocalStorage != null) {
                modifyStoredAppLocalStoragePreference("load-default-create;local-storage");
            }
        }
        return false;
    }

    private boolean loadAppUserSpaceAnyVersionSettings() {
        if (this.mJsAppUuid == null) {
            return false;
        }
        try {
            JSONObject storedJo = getStoredJsonizedData(myAppUserSpaceKeyString());
            if (storedJo == null) {
                storedJo = new JSONObject();
            }
            this.mCurrentAppUserSpaceSettingsAnyVersion = storedJo;
            modifyStoredAppUserSpaceAnyVersionPreference("load-default-create;anyver");
            return true;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return false;
        }
    }

    private boolean loadAppUserSpaceSpecificVersionSettings(String specificVersion) {
        if (this.mJsAppUuid == null || specificVersion == null) {
            return false;
        }
        if (specificVersion.equalsIgnoreCase("*")) {
            return loadAppUserSpaceAnyVersionSettings();
        }
        try {
            JSONObject storedJo = getStoredJsonizedData(myAppUserSpaceKeyString(specificVersion));
            if (storedJo == null) {
                storedJo = new JSONObject();
            }
            this.mCurrentAppUserSpaceSettingsPerVersion.put(specificVersion, storedJo);
            modifyStoredAppUserSpaceVersionPreference(specificVersion, "loadAppUserSpaceSpecificVersionSettings;" + specificVersion);
            return true;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return false;
        }
    }

    public static String readAppAnyVersionSetting(UUID jsAppUuid, String settingName) {
        return readAppAnyVersionSetting(jsAppUuid, settingName, null);
    }

    public static String readAppAnyVersionSetting(UUID jsAppUuid, String settingName, String fallbackSettingValueReturn) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, "*", false);
        if (jsAppPersistentSettings == null) {
            return fallbackSettingValueReturn;
        }
        String cleansedSettingName = settingName.trim();
        return !cleansedSettingName.isEmpty() ? jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion.optString(cleansedSettingName, fallbackSettingValueReturn) : fallbackSettingValueReturn;
    }

    public static String writeAppAnyVersionSetting(UUID jsAppUuid, String settingName, String settingValue) {
        if (jsAppUuid == null || settingName == null || settingValue == null) {
            return null;
        }
        String cleansedSettingName = settingName.trim();
        if (cleansedSettingName.isEmpty()) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        if (jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion == null) {
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion = new JSONObject();
        }
        String prevValue = jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion.optString(cleansedSettingName, settingValue);
        try {
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion.put(cleansedSettingName, settingValue);
            jsAppPersistentSettings.modifyStoredAppUserSpaceAnyVersionPreference("writeAppAnyVersionSetting");
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static String removeAppAnyVersionSetting(UUID jsAppUuid, String settingName) {
        if (jsAppUuid == null || settingName == null) {
            return null;
        }
        String cleansedSettingName = settingName.trim();
        if (cleansedSettingName.isEmpty()) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        if (jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion == null) {
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion = new JSONObject();
        }
        String prevValue = jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion.optString(cleansedSettingName, null);
        try {
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsAnyVersion.remove(cleansedSettingName);
            jsAppPersistentSettings.modifyStoredAppUserSpaceAnyVersionPreference("removeAppAnyVersionSetting");
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static String readAppVersionsSpecificString(UUID jsAppUuid, int specificVersion, String settingName) {
        return readAppVersionSpecificSetting(jsAppUuid, specificVersion, settingName, null);
    }

    public static String readAppVersionSpecificSetting(UUID jsAppUuid, int specificVersion, String settingName, String fallbackSettingValueReturn) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, specificVersion);
        if (jsAppPersistentSettings == null) {
            return fallbackSettingValueReturn;
        }
        String cleansedSettingName = settingName.trim();
        if (cleansedSettingName.isEmpty()) {
            return fallbackSettingValueReturn;
        }
        String specificRv = null;
        try {
            specificRv = ((JSONObject) jsAppPersistentSettings.mCurrentAppUserSpaceSettingsPerVersion.get("" + specificVersion)).optString(cleansedSettingName);
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
        }
        if (specificRv != null) {
            return specificRv;
        }
        return readAppAnyVersionSetting(jsAppUuid, cleansedSettingName, fallbackSettingValueReturn);
    }

    public static String writeAppVersionSpecificSetting(UUID jsAppUuid, int specificVersion, String settingName, String settingValue) {
        if (jsAppUuid == null || settingName == null || settingValue == null) {
            return null;
        }
        String cleansedSettingName = settingName.trim();
        if (cleansedSettingName.isEmpty()) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, specificVersion);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        String specificVersionString = "" + specificVersion;
        JSONObject specificVersionSettingJo = (JSONObject) jsAppPersistentSettings.mCurrentAppUserSpaceSettingsPerVersion.get(specificVersionString);
        if (specificVersionSettingJo == null) {
            specificVersionSettingJo = new JSONObject();
        }
        String prevValue = specificVersionSettingJo.optString(cleansedSettingName, settingValue);
        try {
            specificVersionSettingJo.put(cleansedSettingName, settingValue);
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsPerVersion.put(specificVersionString, specificVersionSettingJo);
            jsAppPersistentSettings.modifyStoredAppUserSpaceVersionPreference(specificVersionString, "writeAppVersionSpecificSetting");
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static String removeAppVersionSpecificSetting(UUID jsAppUuid, int specificVersion, String settingName) {
        if (jsAppUuid == null || settingName == null) {
            return null;
        }
        String cleansedSettingName = settingName.trim();
        if (cleansedSettingName.isEmpty()) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, specificVersion);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        String specificVersionString = "" + specificVersion;
        JSONObject specificVersionSettingJo = (JSONObject) jsAppPersistentSettings.mCurrentAppUserSpaceSettingsPerVersion.get(specificVersionString);
        if (specificVersionSettingJo == null) {
            specificVersionSettingJo = new JSONObject();
        }
        String prevValue = specificVersionSettingJo.optString(cleansedSettingName);
        try {
            specificVersionSettingJo.remove(cleansedSettingName);
            jsAppPersistentSettings.mCurrentAppUserSpaceSettingsPerVersion.put(specificVersionString, specificVersionSettingJo);
            jsAppPersistentSettings.modifyStoredAppUserSpaceVersionPreference(specificVersionString, "removeAppVersionSpecificSetting;" + specificVersion);
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static String readLocalStoragePreference(UUID jsAppUuid, String settingName, String fallbackSettingValueReturn) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, "*", false);
        if (jsAppPersistentSettings != null) {
            try {
                fallbackSettingValueReturn = jsAppPersistentSettings.mCurrentAppLocalStorage.optString(settingName, fallbackSettingValueReturn);
            } catch (Throwable e) {
                Trace.error("JsAppPersistentSettings", e);
            }
        }
        return fallbackSettingValueReturn;
    }

    public static String readLocalStoragePreferences(UUID jsAppUuid) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid, "*", false);
        return jsAppPersistentSettings != null ? jsAppPersistentSettings.mCurrentAppLocalStorage.toString() : new String();
    }

    public static String writeLocalStoragePreference(UUID jsAppUuid, String settingName, String settingValue) {
        if (jsAppUuid == null || settingName == null || settingValue == null) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        if (jsAppPersistentSettings == null || jsAppPersistentSettings.mCurrentAppLocalStorage == null) {
            return null;
        }
        JSONObject localStorageAsJson = jsAppPersistentSettings.mCurrentAppLocalStorage;
        String prevValue = localStorageAsJson.optString(settingName, settingValue);
        try {
            localStorageAsJson.put(settingName, settingValue);
            jsAppPersistentSettings.mCurrentAppLocalStorage = localStorageAsJson;
            jsAppPersistentSettings.modifyStoredAppLocalStoragePreference("writeLocalStoragePreference");
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static String clearLocalStoragePreference(UUID jsAppUuid, String settingName) {
        if (jsAppUuid == null || settingName == null) {
            return null;
        }
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings == null) {
            return null;
        }
        if (jsAppPersistentSettings.mCurrentAppLocalStorage == null) {
            jsAppPersistentSettings.mCurrentAppLocalStorage = new JSONObject();
        }
        String prevValue = jsAppPersistentSettings.mCurrentAppLocalStorage.optString(settingName, null);
        try {
            jsAppPersistentSettings.mCurrentAppLocalStorage.remove(settingName);
            jsAppPersistentSettings.modifyStoredAppLocalStoragePreference("clearLocalStoragePreference");
            return prevValue;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    public static void clearLocalStoragePreferences(UUID jsAppUuid) {
        if (jsAppUuid != null) {
            JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
            if (jsAppPersistentSettings != null) {
                if (jsAppPersistentSettings.mCurrentAppLocalStorage == null) {
                    jsAppPersistentSettings.mCurrentAppLocalStorage = new JSONObject();
                }
                jsAppPersistentSettings.modifyStoredAppLocalStoragePreference("clearLocalStoragePreferences");
            }
        }
    }

    public static boolean queryAppEnabled(UUID jsAppUuid) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings != null) {
            return jsAppPersistentSettings.queryEnabled();
        }
        return false;
    }

    public static LaunchOnMessageSettingE querySystemGlobalLaunchOnMessage() {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(NullUuid);
        if (jsAppPersistentSettings != null) {
            return jsAppPersistentSettings.queryLaunchOnMessage();
        }
        return LaunchOnMessageSettingE._INVALID_;
    }

    public static LaunchOnMessageSettingE queryAppLaunchOnMessage(UUID jsAppUuid) {
        JsAppPersistentSettings jsAppPersistentSettings = tryLoad(jsAppUuid);
        if (jsAppPersistentSettings != null) {
            return jsAppPersistentSettings.queryLaunchOnMessage();
        }
        return LaunchOnMessageSettingE._INVALID_;
    }

    public boolean queryEnabled() {
        if (this.mCurrentSystemSettings != null) {
            return this.mCurrentSystemSettings.optBoolean("enabled", false);
        }
        if (load()) {
            return this.mCurrentSystemSettings.optBoolean("enabled", false);
        }
        return false;
    }

    public LaunchOnMessageSettingE queryLaunchOnMessage() {
        LaunchOnMessageSettingE rv;
        if (this.mCurrentSystemSettings == null) {
            if (load()) {
                try {
                    rv = LaunchOnMessageSettingE.valueOf(this.mCurrentSystemSettings.getString("lom"));
                    if (rv != null) {
                        return rv;
                    }
                } catch (Throwable e) {
                    Trace.error("JsAppPersistentSettings", e);
                }
            }
            return LaunchOnMessageSettingE.NotDeclared;
        }
        try {
            rv = LaunchOnMessageSettingE.valueOf(this.mCurrentSystemSettings.getString("lom"));
            if (rv != null) {
                return rv;
            }
        } catch (Throwable e2) {
            Trace.error("JsAppPersistentSettings", e2);
        }
        return LaunchOnMessageSettingE.NotDeclared;
    }

    private static JSONObject defaultSystemGlobalSettings() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("u", new UUID(0, 0).toString());
            jsonObject.put("v", 1);
            jsonObject.put("enabled", true);
            jsonObject.put("lom", LaunchOnMessageSettingE.No);
            return jsonObject;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    private static JSONObject defaultSettings(UUID jsAppUuid) {
        if (jsAppUuid == null) {
            return defaultSystemGlobalSettings();
        }
        if (jsAppUuid.equals(NullUuid)) {
            return defaultSystemGlobalSettings();
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("u", jsAppUuid.toString());
            jsonObject.put("v", 1);
            jsonObject.put("enabled", true);
            jsonObject.put("lom", LaunchOnMessageSettingE.No);
            return jsonObject;
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return null;
        }
    }

    private static String formKeystring(UUID jsAppUuid) {
        return "JsAppPersistentSettings_" + (jsAppUuid == null ? "" : jsAppUuid.toString());
    }

    private static String formAppUserSpaceKeystring(UUID jsAppUuid, String appVersion) {
        return "app_settings_storage" + (jsAppUuid == null ? "" : jsAppUuid.toString()) + "_" + appVersion;
    }

    private static String formAppLocalStorageKeystring(UUID jsAppUuid) {
        return "hls_" + (jsAppUuid == null ? "" : jsAppUuid.toString());
    }

    private String myKeyString() {
        return formKeystring(this.mJsAppUuid);
    }

    private String myAppUserSpaceKeyString() {
        return formAppUserSpaceKeystring(this.mJsAppUuid, "*");
    }

    private String myAppUserSpaceKeyString(String specificVersion) {
        UUID uuid = this.mJsAppUuid;
        if (specificVersion == null) {
            specificVersion = "*";
        }
        return formAppUserSpaceKeystring(uuid, specificVersion);
    }

    private String myAppLocalStorageKeyString() {
        return formAppLocalStorageKeystring(this.mJsAppUuid);
    }

    public void modifyStoredSystemPreference(String traceContextData) {
        try {
            setJsonizedData(myKeyString(), this.mCurrentSystemSettings);
        } catch (Exception e) {
            if (traceContextData == null) {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified setting (no trace context data provided); exc = " + e.getMessage());
            } else {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified setting; trace context data = " + traceContextData + "; exc = " + e.getMessage());
            }
        }
    }

    public void modifyStoredAppUserSpaceAnyVersionPreference(String traceContextData) {
        try {
            setJsonizedData(myAppUserSpaceKeyString(), this.mCurrentAppUserSpaceSettingsAnyVersion);
        } catch (Exception e) {
            if (traceContextData == null) {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified setting (no trace context data provided); exc = " + e.getMessage());
            } else {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified setting; trace context data = " + traceContextData + "; exc = " + e.getMessage());
            }
        }
    }

    public void modifyStoredAppUserSpaceVersionPreference(String specificVersion, String traceContextData) {
        try {
            setJsonizedData(myAppUserSpaceKeyString(specificVersion), (JSONObject) this.mCurrentAppUserSpaceSettingsPerVersion.get(specificVersion));
        } catch (Exception e) {
            if (traceContextData == null) {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified per-version app setting (no trace context data provided); exc = " + e.getMessage());
            } else {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified per-version app setting; trace context data = " + traceContextData + "; exc = " + e.getMessage());
            }
        }
    }

    public void modifyStoredAppLocalStoragePreference(String traceContextData) {
        try {
            setJsonizedData(myAppLocalStorageKeyString(), this.mCurrentAppLocalStorage);
        } catch (Exception e) {
            if (traceContextData == null) {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified local storage setting (no trace context data provided); exc = " + e.getMessage());
            } else {
                Trace.debug("JsAppPersistentSettings", "Failed to store modified local storage setting; trace context data = " + traceContextData + "; exc = " + e.getMessage());
            }
        }
    }

    public static JSONObject getStoredJsonizedData(String key) {
        return getStoredJsonizedData(key, null);
    }

    public static JSONObject getStoredJsonizedData(String key, JSONObject fallbackReturnData) {
        String stringEncodedJsonObject = PblPreferences.getSharedPreferences(JsAppContextBridge.getInstance().getAppContext()).getString(key, null);
        if (TextUtils.isEmpty(stringEncodedJsonObject)) {
            return fallbackReturnData;
        }
        try {
            return new JSONObject(stringEncodedJsonObject);
        } catch (Throwable e) {
            Trace.error("JsAppPersistentSettings", e);
            return fallbackReturnData;
        }
    }

    public void setJsonizedData(String key, JSONObject jsonObject) {
        if (key != null && jsonObject != null) {
            try {
                setJsonizedData(key, jsonObject.toString(), false);
            } catch (Throwable e) {
                Trace.error("JsAppPersistentSettings", e);
            }
        }
    }

    public static boolean setJsonizedData(String key, String stringEncodedJsonObject, boolean verifyJson) {
        if (key == null || stringEncodedJsonObject == null) {
            return false;
        }
        if (verifyJson) {
            try {
                JSONObject jSONObject = new JSONObject(stringEncodedJsonObject);
            } catch (Throwable e) {
                Trace.error("JsAppPersistentSettings", e);
                return false;
            }
        }
        setStringData(key, stringEncodedJsonObject);
        return true;
    }

    private static void setStringData(String key, String data) {
        if (key != null) {
            PblPreferences.getSharedPreferences(JsAppContextBridge.getInstance().getAppContext()).edit().putString(key, data).commit();
        }
    }
}
